/*
 * @Author: 徐建辰
 * @Date: 2022-04-15 14:02:35
 * @LastEditTime: 2022-06-23 09:28:19
 * @LastEditors: xjc
 * @Description: vue-cli5配置
 */
const {defineConfig} = require('@vue/cli-service')
const path = require('path')
const resolve = dir => path.join(__dirname, dir)

const qiniuCdn = 'https://cdn.staticfile.org'
const cdn = {
  css: [
    `${qiniuCdn}/element-plus/2.1.9/index.min.css`,
    `${qiniuCdn}/element-plus/2.1.9/theme-chalk/display.min.css`,
    `${qiniuCdn}/nprogress/0.2.0/nprogress.min.css`
  ],
  js: [
    `${qiniuCdn}/vue/3.2.36/vue.global.prod.min.js`,
    `${qiniuCdn}/vue-router/4.0.13/vue-router.global.prod.min.js`,
    `${qiniuCdn}/axios/0.26.1/axios.min.js`,
    `${qiniuCdn}/xlsx/0.17.4/xlsx.min.js`,
    `${qiniuCdn}/nprogress/0.2.0/nprogress.min.js`,
    `${qiniuCdn}/element-plus/2.1.9/index.full.min.js`
  ]
}

module.exports = defineConfig({
  assetsDir: './',
  publicPath: './',
  transpileDependencies: true,
  productionSourceMap: false,
  chainWebpack: config => {
    // 移除 prefetch 插件
		config.plugins.delete('preload')
		config.plugins.delete('prefetch')
    // cdn配置
    if (process.env.NODE_ENV === 'production') {
      config.externals({
        'vue': 'Vue',
        'vue-router': 'VueRouter',
        'axios': 'axios',
        'xlsx': 'XLSX',
        'countup': 'CountUp',
        'nprogress': 'NProgress',
        'element-plus': 'ElementPlus',
        'cropperjs': 'CropperJs'
      })
      // 注入cdn变量 (打包时会执行)
      config.plugin('html').tap(args => {
        args[0].cdn = cdn
        return args
      })
      // 添加打包分析工具
      if (process.env.use_analyzer) {
        config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
      }
    }
    // 配置svg
    config.module
      .rule('svg')
      .exclude.add(resolve('./src/assets/icons/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('./src/assets/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({symbolId: 'icon-[name]'})
      .end()
  },
  css: {
    loaderOptions: {
      sass: {
        additionalData: `
          @import '@/assets/style/var.scss';
          @import '@/assets/style/mixin.scss';
        `
      }
    }
  },
  devServer: {
    host: 'localhost',
    port: 8080,
    proxy: {
      '/agency': {
        target: 'https://test.hnchengyue.com/charging',
        changeOrigin: true,
        secure: false,
        pathRewrite: {'^/agency': ''}
      }
    }
  },
  configureWebpack: config => {
    config.externals = 
    // 性能提示
		config.performance = {
			hints: false
		},
    config.optimization = {
      splitChunks: {
        chunks: 'all',
				automaticNameDelimiter: '~',
				name: false,
        cacheGroups: {
          // 第三方库抽离
					vendor: {
						name: 'modules',
						test: /[\\/]node_modules[\\/]/,
						priority: -10
					},
					elicons: {
						name: 'elicons',
						test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]/
					},
					tinymce: {
						name: 'tinymce',
						test: /[\\/]node_modules[\\/]tinymce[\\/]/
					},
					// echarts: {
					// 	name: 'echarts',
					// 	test: /[\\/]node_modules[\\/]echarts[\\/]/
					// },
					xgplayer: {
						name: 'xgplayer',
						test: /[\\/]node_modules[\\/]xgplayer.*[\\/]/
					}
        }
      }
    }
  }
})
